<#setting classic_compatible="true">
<style type="text/css">
    .moduleadd {
        margin: 20px 0px;
    }

    .moduleadd .extinfo {
        margin: 10px 0px;
    }

    .moduleadd .extinfo h1 {
        font-size: 20px;
    }

    .moduleadd .extinfo .desc {
        margin-top: 10px;
    }

    .moduleadd .title {
        min-width: 500px;
    }

    .moduleadd .position {
        min-width: 200px;
    }

    .moduleadd .index {
        width: 80px;
    }

    .moduleadd .moduleoption {
        margin-top: 15px;
        min-height: 300px;
        font-size: 1em;
    }

    .moduleadd .moduleoption ul {
        /* padding: 0px 10px;
         background: none;
         border: 0px;
         border-bottom: 1px solid #4297D7;
         border-radius: 0px;*/
    }

    .moduleadd .moduleoption .chose span {
        cursor: pointer;
    }

    .moduleadd .moduleoption .channel select {
        width: 200px;
    }

    .moduleadd .moduleoption .channellist {
        display: none;
    }
</style>
<script type="text/javascript">
    $(document).ready(function (e) {
        $('.moduleoption > ul a').each(function (i, e) {
            $(this).attr('href', location.pathname + $(this).attr('href'));
        });
        $('.editor').xheditor({tools: 'full', height: 300})
        $('.moduleoption').tabs();
        var channeltree = $('.channeltree').jTree();
        channeltree.showAll(0);
        setchannellist();
    });
    function setchannellist() {
    <#if module.channel>
        $('select[name=channel_all]').val('${module.channel.all}');
        $('select[name=channel_all]').trigger('change');
        var list =${module.channel.list};
        for (var i = 0; i < list.length; i++) {
            $('input[type=checkbox][name=channel_list][value=' + list[i] + ']').attr('checked', true);
        }
    </#if>
    }
    function module_save(func) {
        if (checkForm($('.moduleadd *[name]'))) {
            var data = serializeArray($('.moduleadd .base *[name]'));
            data = serializeArray($('.moduleadd .webnode *[name]'), data);
            data.push({
                name: 'option',
                value: JSON.stringify(LC.arrayToJson(LC.serializeArray($('.moduleadd .option *[name]'))))
            });
            $.ajax({
                url: 'system/module.action?act=save',
                type: 'POST',
                data: data,
                dataType: "JSON",
                success: function (rs) {
                    if (rs.code == 0 || rs.code == 1) {
                        $('.moduleadd input[name=id]').val(rs.id);
                        if (func != undefined) {
                            func(rs);
                        } else {
                            alert('保存成功!');
                        }
                    } else {
                        alert(rs.msg);
                    }
                }
            });
        }
    }
    function module_save_close() {
        module_save(module_cancel);
    }
    function module_save_new() {
        module_save(function (rs) {
            goPage('system/module.action?act=add&key=${extension.key}', '#main')
        });
    }
    function module_cancel() {
        goPage('system/module.action', '#main');
    }
    function changecb(_this) {
        var ch = $(_this).prev();
        ch.trigger('click');
    }
    function changechannellist(_this) {
        if ($(_this).val() == '*') {
            $('.channellist').hide();
        } else {
            $('.channellist').show();
        }
    }
</script>
<div class="toolbar">
    <button type="button" onClick="module_save()"><span class="icon-edit"></span> 保存</button>
    <button type="button" onClick="module_save_close()"><span class="icon-checkmark"></span> 保存并关闭</button>
    <button type="button" onClick="module_save_new()"><span class="icon-plus"></span> 保存并新建</button>
    <button type="button" onClick="module_cancel();"><span class="icon-close"></span> 取消</button>
</div>
<div class="moduleadd">
    <div class="base">
        <input type="hidden" name="id" isnull value="${module._id}"/>
        <input type="hidden" name="extensionkey" isnull value="${extension.key}"/>

        <div class="extinfo">
            <h1>${extension.name}</h1>

            <div class="desc">${extension.description}</div>
        </div>
        <div class="group">
            <div class="label">标题&nbsp;*</div>
            <div class="value">
                <input name="title" class="title" type="text" regx="${check.title}" maxlength="100"
                       value="${module.title}"/>
            </div>
        </div>
        <div class="group">
            <div class="label">位置&nbsp;*</div>
            <div class="value">
                <input name="position" class="position" type="text" regx="${check.position}" maxlength="100"
                       value="${module.position}"/>
            </div>
        </div>
        <div class="group">
            <div class="label">优先级&nbsp;*</div>
            <div class="value">
                <input name="index" class="index" type="number" regx="${check.index}" value="${module.index!0}"/>
            </div>
        </div>
    </div>
    <div class="moduleoption">
        <ul>
        <#list extension.config as cf>
            <li><a href="#${cf.name}">${cf.name}</a></li>
        </#list>
            <li><a href="#channel">栏目分配</a></li>
        </ul>
    <#list extension.config as cf>
        <div class="tabcontentchild option" id="${cf.name}">
            <#list cf.option as o>
                <#switch o.type>
                    <#case "editor">
                        <div class="">
                            <textarea isnull name="${o.name}" class="editor"
                                      style="width:100%">${module.option[o.name]!o.default}</textarea>
                        </div>
                        <#break><#default>
                </#switch>
            </#list>
        </div>
    </#list>
        <div class="tabcontentchild channel" id="channel">
            <div class="group">
                <div class="label">分配方式</div>
                <div class="value">
                    <select class="selectmenu" name="channel_all" onChange="changechannellist(this)">
                        <option value="*">所有页面</option>
                        <option value="+">所有页面，选定页面除外</option>
                        <option value="-">只在选定的页面</option>
                    </select>
                </div>
            </div>
            <div class="group channellist">
                <div class="label">分配页面</div>
                <div class="value">
                    <div class="chose"><input type="checkbox"
                                              onClick="cbchange(this,$('input[type=checkbox][name=channel_list]'))"/>&nbsp;<span
                            onClick="changecb(this)">全选</span>/<span
                            onClick="cbAnti($('input[type=checkbox][name=channel_list]'))">反选</span></div>
                    <div class="channeltree">
                    <#list channels as m>
                        <div id="${m._id}" pid="${m.parentid}">
                            <img src="../images/tree_tip.png">
                            <span class="name">${m.name}</span>
                            <input type="checkbox" name="channel_list" value="${m._id}"/>
                        </div>
                    </#list>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>